package edu.udlap.spring2008.is300.finalexam.app.mvc.controller;

import edu.udlap.spring2008.is300.finalexam.app.mvc.model.database.objects.AppCategoryRecord;
import edu.udlap.spring2008.is300.finalexam.app.mvc.model.database.objects.AppFormatRecord;
import edu.udlap.spring2008.is300.finalexam.app.mvc.model.database.objects.AppImageRecord;
import edu.udlap.spring2008.is300.finalexam.app.mvc.model.database.objects.AppPrivacyLevelRecord;
import edu.udlap.spring2008.is300.finalexam.app.mvc.model.database.objects.AppSizeRecord;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.controller.Controller;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.Model;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.database.filters.ImageRecordFilter;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.database.objects.ImageRecord;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.view.ViewComponent;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Ruslán Ledesma Garza
 */
public class AppController extends Controller
{

    public AppController(Model model)
    {
        this(null, model);
    }
    
    public AppController(ViewComponent view, Model model)
    {
        super(view, model);
    }

    @Override
    public void setInitialState()
    {
        super.setInitialState();
        setState(new AppControllerGridModeState(this));
    }
    
    @Override
    public void showAddNewDialog()
    {
        if(model_ == null)
            return;
        
        JFileChooser openDialog =
                new JFileChooser();
        
        FileNameExtensionFilter fileFilter = new FileNameExtensionFilter("Image files",
                "jpg", "gif", "png", "bmp");
        
        openDialog.setFileFilter(fileFilter);
        
        if(openDialog.showOpenDialog(view_.getComponent()) == 
                JFileChooser.APPROVE_OPTION)
        {
            ImageRecord imageRecord = new AppImageRecord(
                    0, openDialog.getSelectedFile().getPath(),
                    false, "",
                    new AppCategoryRecord(1, ""),
                    new AppSizeRecord(1, ""),
                    new AppFormatRecord(1, ""),
                    new AppPrivacyLevelRecord(1, ""));
            
            model_.addImageRecord(imageRecord);
        }
    }

    @Override
    public void addImageRecord(ImageRecord imageRecord)
    {
        if(model_ == null)
            return;
        
        model_.addImageRecord(imageRecord);
    }

    @Override
    public void deleteSelectedImageRecord()
    {
        if(model_ == null)
            return;
        
        model_.deleteSelectedImageRecord();
    }

    @Override
    public void updateImageRecord(ImageRecord imageRecord)
    {
        if(model_ == null)
            return;
        
        model_.updateImageRecord(imageRecord);
    }

    @Override
    public void setImageRecordFilter(ImageRecordFilter imageRecordFilter)
    {
        if(model_ == null)
            return;
        
        model_.setImageRecordFilter(imageRecordFilter);
    }

    @Override
    public void setSelectedImageRecord(int id)
    {
        if(model_ == null)
            return;
        
        model_.setSelectedImageRecord(id);
    }

    @Override
    public void toggleFavoriteStatusOfSelectedImageRecord()
    {
        if(model_ == null)
            return;
        
        ImageRecord imageRecord = model_.getSelectedImageRecord();
        
        imageRecord.setFavorite(!imageRecord.isFavorite());
        
        model_.updateImageRecord(imageRecord);
    }

    // <editor-fold desc="operations (view behavior provided) by the controller (strategy)...">
    AppControllerState currentState_;
    
    void setState(AppControllerState state)
    {
        currentState_ = state;
    }
    // </editor-fold>
    
    @Override
    public void setGridMode()
    {
        currentState_.setGridMode();
    }

    @Override
    public void setSlideShowMode()
    {
        currentState_.setSlideMode();
    }

    @Override
    public void toggleGridSlideMode()
    {
        currentState_.toggleGridSlideMode();
    }

    @Override
    public void nextImageRecord()
    {
        ArrayList<ImageRecord> imageRecords = 
                model_.getMatchingImageRecords();
        ImageRecord imageRecord =
                model_.getSelectedImageRecord();
        
        int i = imageRecords.indexOf(imageRecord) + 1;
        
        if(i >= imageRecords.size()) i = imageRecords.size() - 1;
        
        model_.setSelectedImageRecord(imageRecords.get(i).getId());
    }

    @Override
    public void previousImageRecord()
    {
        ArrayList<ImageRecord> imageRecords = 
                model_.getMatchingImageRecords();
        ImageRecord imageRecord =
                model_.getSelectedImageRecord();
        
        int i = imageRecords.indexOf(imageRecord) - 1;
        
        if(i < 0) i = 0;
        
        model_.setSelectedImageRecord(imageRecords.get(i).getId());
    }

    @Override
    public void firstImageRecord()
    {
        ArrayList<ImageRecord> imageRecords = 
                model_.getMatchingImageRecords();
        
        model_.setSelectedImageRecord(imageRecords.get(0).getId());
    }

    @Override
    public void lastImageRecord()
    {
        ArrayList<ImageRecord> imageRecords = 
                model_.getMatchingImageRecords();
        ImageRecord imageRecord =
                model_.getSelectedImageRecord();
        
        int i = imageRecords.size() - 1;
        
        model_.setSelectedImageRecord(imageRecords.get(i).getId());
    }
    
}
